<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function addLargeNumbers(str1, str2) {
            let num1 = str1, num2 = str2
            // -a+-b=-a-b=-(a+b)
            if (str1.startsWith('-') && str2.startsWith('-')) {
                return '-' + addLargeNumbers(str1.substring(1), str2.substring(1))
            } else if (str1.startsWith('-')) {
                // -a+b=b-a
                return subtractLargeNumbers(str2, str1.substring(1))
            } else if (str2.startsWith('-')) {
                // a+-b=a-b
                return subtractLargeNumbers(str1, str2.substring(1))
            }
            let maxLength = Math.max(num1.length, num2.length);
            let result = '';
            let carry = 0;

            for (let i = 0; i < maxLength; i++) {
                let digit1 = i < num1.length ? parseInt(num1[num1.length - 1 - i]) : 0;
                let digit2 = i < num2.length ? parseInt(num2[num2.length - 1 - i]) : 0;
                let sum = digit1 + digit2 + carry;
                carry = Math.floor(sum / 10);
                result = (sum % 10) + result;
            }

            if (carry > 0) {
                result = carry + result;
            }

            return result;
        }

        // // 测试
        let num1 = '99999999999999999999999999';
        let num2 = '-88888888888888888888888888';
        let sum = addLargeNumbers(num1, num2);
        console.log(sum);


        // 相减
        function subtractLargeNumbers(str1, str2) {
            let num1 = str1, num2 = str2
            if (str1.startsWith('-') && str2.startsWith('-')) {
                // -a- -b=-a+b=b-a
                return subtractLargeNumbers(str2.substring(1), str1.substring(1))
            } else if (str1.startsWith('-')) {
                // -a-b=-(a+b)
                return '-' + addLargeNumbers(str1.substring(1), str2)
            } else if (str2.startsWith('-')) {
                // a- -b=a+b
                return addLargeNumbers(str1, str2.substring(1))
            }
            let maxLength = Math.max(num1.length, num2.length);
            let result = '';
            let borrow = 0;

            // 确保大-小
            if (str1.length === str2.length && str2[0] > str1[0] || str1.length < str2.length) {
                num1 = str2
                num2 = str1
            }
            for (let i = 0; i < maxLength; i++) {
                let digit1 = i < num1.length ? parseInt(num1[num1.length - 1 - i]) : 0;
                let digit2 = i < num2.length ? parseInt(num2[num2.length - 1 - i]) : 0;
                let diff = digit1 - digit2 - borrow;

                if (diff < 0) {
                    diff += 10;
                    borrow = 1;
                } else {
                    borrow = 0;
                }

                result = diff + result;
            }

            // 移除结果前面的零
            // result = result.replace(/^0+/, '');
            // if (num2.length === maxLength) {
            //     result = '-' + result
            // }
            return result;
        }
        // 测试

        let difference = subtractLargeNumbers(num1, num2);
        console.log(difference);
    </script>
</body>

</html>